package com.ruoyi.gczl.sybg.ws.bl.controller;

import com.ruoyi.common.GetFileName;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.sybg.ws.bl.domain.SybgBlws;
import com.ruoyi.gczl.sybg.ws.bl.service.ISybgBlwsService;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 试验报告巴拉无损Controller
 *
 * @author huacai
 * @date 2024-08-23
 */
@RestController
@RequestMapping("/sybg/blws")
public class SybgBlwsController extends BaseController {
    @Resource
    private ISybgBlwsService sybgBlwsService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询试验报告巴拉无损列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:list')")
    @GetMapping("/list")
    public TableDataInfo list(SybgBlws sybgBlws) {
        startPage();
        List<SybgBlws> list = sybgBlwsService.selectSybgBlwsList(sybgBlws);
        return getDataTable(list);
    }

    /**
     * 导出试验报告巴拉无损列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:export')")
    @Log(title = "试验报告巴拉无损", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SybgBlws sybgBlws) {
        List<SybgBlws> list = sybgBlwsService.selectSybgBlwsList(sybgBlws);
        ExcelUtil<SybgBlws> util = new ExcelUtil<SybgBlws>(SybgBlws.class);
        util.exportExcel(response, list, "试验报告巴拉无损数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SybgBlws> util = new ExcelUtil<SybgBlws>(SybgBlws.class);
        util.importTemplateExcel(response, "试验报告巴拉无损数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "试验报告巴拉无损", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sybg:blws:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SybgBlws> util = new ExcelUtil<SybgBlws>(SybgBlws.class);
        InputStream inputStream = file.getInputStream();
        List<SybgBlws> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sybgBlwsService.batchInsertSybgBlws(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取试验报告巴拉无损详细信息
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:query')")
    @GetMapping(value = "/{sybgBlwsId}")
    public AjaxResult getInfo(@PathVariable("sybgBlwsId") Integer sybgBlwsId) {
        return success(sybgBlwsService.selectSybgBlwsBySybgBlwsId(sybgBlwsId));
    }

    /**
     * 新增试验报告巴拉无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:add')")
    @Log(title = "试验报告巴拉无损", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SybgBlws sybgBlws) {
        sybgBlws.setUserId(Math.toIntExact(getUserId()));
        sybgBlws.setDeptId(Math.toIntExact(getDeptId()));
        sybgBlws.setCreateBy(getUsername());
        int i = sybgBlwsService.insertSybgBlws(sybgBlws);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(String.valueOf(sybgBlws.getSybgBlwsId()));
        infoRunTaskData.setBusinessType(9); //业务模块: 试验报告-无损-巴拉
        String fileNameOnly = GetFileName.getFileNameOnly(sybgBlws.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 合同名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改试验报告巴拉无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:edit')")
    @Log(title = "试验报告巴拉无损", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SybgBlws sybgBlws) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        String fileNameOnly = GetFileName.getFileNameOnly(sybgBlws.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 合同名称
        infoRunTaskData.setBusinessKey(String.valueOf(sybgBlws.getSybgBlwsId()));
        infoRunTaskData.setBusinessType(9); //业务模块: 试验报告-无损-巴拉
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(sybgBlwsService.updateSybgBlws(sybgBlws));
    }

    /**
     * 删除试验报告巴拉无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:blws:remove')")
    @Log(title = "试验报告巴拉无损", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sybgBlwsIds}")
    public AjaxResult remove(@PathVariable Integer[] sybgBlwsIds) {
        //同时删除业务关系数据
        for (Integer sybgBlwsId : sybgBlwsIds) {
            SybgBlws sybgBlws = sybgBlwsService.selectSybgBlwsBySybgBlwsId(sybgBlwsId);
            iInfoRunTaskDataService.deleteData(String.valueOf(sybgBlws.getSybgBlwsId()), 9);//业务模块: 试验报告-无损-巴拉
        }
        return toAjax(sybgBlwsService.deleteSybgBlwsBySybgBlwsIds(sybgBlwsIds));
    }
}
